

  let form = layui.form
function linkList() {
    $.ajax({
        type: 'GET',
        url: '/api/links',
        success: function (res) {
            // console.log(res)
            let str = ''
            res.data.forEach(item => {
                let image = ` <img src="../../前台初始代码/uploads/${item.linkicon}" alt=""></img`
                str += `<tr>
                <td>${item.id}</td>
                <td>${image}</td>
                <td>${item.linkname}</td>
                <td>${item.linkurl}</td>
                <td>${item.linkdesc}</td>
                <td> <div class="layui-form-item">
                <div class=" center">
                  <button type="reset" data-id="${item.id}" data-name="${item.linkname}" data-url="${item.linkurl}" data-icon="${item.linkicon}"class="layui-btn layui-btn layui-btn-xs write" lay-submit lay-filter="formDemo ">编辑</button>
                  <button type="reset" data-id="${item.id}" class="layui-btn  layui-btn layui-btn-xs layui-btn layui-btn layui-btn-danger del">删除</button>
                </div>
              </div></td>
                </tr>`   
            });
            $('.layui-table tbody').html(str)
       }
    })
}
linkList()

// 添加内容
$('#add-link').on('click', function (e) {
  // 添加弹层
  var index = layer.open({
    type: 1,
    title: '添加友情链接',
    content: $('#add-form-tpl').html(),
    area: ['500px', '350px']
  })

  $('#urlIcon').on('click', function () {
    $('#linkFile').trigger('click')
   
  })

// 图片文件改变
  $('#linkFile').on('change', function () {
    console.dir(this)
    if (this.files.length > 0) {
      let fileObj = this.files[0]
       // 把文件转换为服务器认识的URL
      let url = URL.createObjectURL(fileObj)
      // console.log(url);
      $('#preIcon').attr('src', url)

    }
  })

  //添加链接
  $('#add-form').on('submit',function (e) {
    e.preventDefault()
    var fd = new FormData(this)
      console.dir(fd)
      $.ajax({
        type: 'POST',
        url: 'admin/links',
        data: fd,
        processData: false,
        contentType: false,
        success: function (res) {
          console.log(res)
          if (res.status === 0) {
            // 关闭窗口
            layer.close(index)
            // 刷新列表
            linkList()
          }
        }
      })
  })
 
})


 // 删除链接
 $('.layui-table tbody').on('click', '.del', function () {
  let id = $(this).data('id')
  layer.confirm('确定要删除吗？', { icon: 3, title: '提示' }, function (index) {
    $.ajax({
      type:'DELETE',
      url: '/my/article/delete/' + id,  
      success: function (res) {
        layer.msg(res.message)
        if (res.status === 0) {
          layer.close(index)
          linkList();
        }
      }
    });
   
  });
  
 })

// 编辑链接
$('.layui-table tbody').on('click', '.write', function () {
  let id = $(this).data('id')
  let data = $(this).data()
  // console.log(id)
  // console.log(data)
  var index = layer.open({
    type: 1,
    title: '添加友情链接',
    content: $('#edit-form-tpl').html(),
    area: ['500px', '350px'],
    success: function (res) {

      let form = layui.form;
      // 数据回填
      form.val('editForm', res.data);
    }
  })

  $('#urlIcon').on('click', function () {
    $('#linkFile').trigger('click')
   
  })
  $('#linkFile').on('change', function () {
    console.dir(this)
    if (this.files.length > 0) {
      let fileObj = this.files[0]
       // 把文件转换为服务器认识的URL
      let url = URL.createObjectURL(fileObj)
      // console.log(url);
      $('#preIcon').attr('src', url)

    }
  })
  $.ajax({
    type: 'PUT',
    url: 'admin/links/' + id,
    data: data,
    success: function (res) {
      console.log(res)
      
    }
  })
})
